package com.xique.door.device.hq.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xique.door.constant.DoorDeviceConstant;
import com.xique.door.device.hq.api.IHqDeviceApi;
import com.xique.door.device.hq.bean.constant.HqDeviceConstant;
import com.xique.door.device.hq.bean.request.HqDeviceDelPersonRequest;
import com.xique.door.device.hq.bean.request.HqDeviceDownPersonRequest;
import com.xique.door.device.hq.bean.request.HqDeviceDownPersonsRequest;
import com.xique.door.device.hq.bean.request.HqDeviceGetStatusRequest;
import com.xique.door.device.hq.service.IHqDeviceCommandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author shd
 * @create 2021/9/17 14:07
 */
@Service
public class HqDeviceCommandServiceImpl implements IHqDeviceCommandService {

    @Autowired
    private IHqDeviceApi hqDeviceApi;

    private Map<String, Object> getBaseMap(String serviceName, String version) {
        Map<String, Object> map = new HashMap<>(5);
        map.put("charset","UTF-8");
        map.put("service",serviceName);
        map.put("version",version);
        map.put("sign","sign");
        return map;
    }

    @Override
    public JSONObject downPersonCommandIssue(HqDeviceDownPersonRequest request) {
        Map<String, Object> map = getBaseMap(HqDeviceConstant.CommandFace.CommandDownPerson, request.getVersion());
        Map<String, Object> dataMap = new HashMap<>(3);
        dataMap.put("deviceCodes", request.getDeviceSns());
        dataMap.put("type", DoorDeviceConstant.DeviceModel.HQ);
        dataMap.put("info", request.getInfo());
        map.put("data", dataMap);
        return hqDeviceApi.downPerson(map);
    }

    @Override
    public JSONObject downPersonsCommandIssue(HqDeviceDownPersonsRequest request) {
        Map<String, Object> map = getBaseMap(HqDeviceConstant.CommandFace.CommandDownPersons, request.getVersion());
        Map<String, Object> dataMap = new HashMap<>(3);
        dataMap.put("deviceCodes", request.getDeviceSns());
        dataMap.put("type", DoorDeviceConstant.DeviceModel.HQ);
        dataMap.put("info", request.getInfo());
        map.put("data", dataMap);
        return hqDeviceApi.downPersons(map);
    }

    @Override
    public JSONObject delPersonCommandIssue(HqDeviceDelPersonRequest request) {
        Map<String, Object> map = getBaseMap(HqDeviceConstant.CommandFace.CommandDelPerson, request.getVersion());
        Map<String, Object> dataMap = new HashMap<>(3);
        dataMap.put("deviceCodes", request.getDeviceSns());
        dataMap.put("type", DoorDeviceConstant.DeviceModel.HQ);
        dataMap.put("info", request.getInfo());
        map.put("data", dataMap);
        return hqDeviceApi.delPerson(map);
    }

    @Override
    public JSONObject getStatusCommandIssue(HqDeviceGetStatusRequest request) {
        Map<String, Object> map = getBaseMap(HqDeviceConstant.CommandFace.CommandGetStatus, request.getVersion());
        Map<String, Object> dataMap = new HashMap<>(3);
        dataMap.put("deviceCodes", request.getDeviceSns());
        dataMap.put("class_name", request.getClassName());
        dataMap.put("customIds", request.getCustomIds());
        map.put("data", dataMap);
        return hqDeviceApi.getStatus(map);
    }
}
